﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Windows.Forms;
using System.Data.Objects.SqlClient;
using System.Text;
using DTO;
using AutoMapper;

namespace DAO
{
    public class PhieuTraRepository
    {
        public static PhieuTraDTO CreatePhieuTraDTOFromPhieuTraEF(PHIEU_TRA phieuTra)
        {
            PhieuTraDTO phieuTraDTO = new PhieuTraDTO();
            Mapper.CreateMap<PHIEU_TRA, PhieuTraDTO>();
            phieuTraDTO = Mapper.Map<PHIEU_TRA, PhieuTraDTO>(phieuTra);
            return phieuTraDTO;
        }

        public static PHIEU_TRA CreatePhieuTraEFFromPhieuTraDTO(PhieuTraDTO phieuTraDTO)
        {
            PHIEU_TRA phieuTra = new PHIEU_TRA();
            Mapper.CreateMap<PhieuTraDTO, PHIEU_TRA>();
            phieuTra = Mapper.Map<PhieuTraDTO, PHIEU_TRA>(phieuTraDTO);
            return phieuTra;
        }

        public int LayMaPhieuTraMax()
        {
            int maPhieuTra = 0;
            try
            {
                QLTVEntities context = ContextFactory.GetInstance();
                maPhieuTra = context.PHIEU_TRAList.Max(s => s.MaPhieuTra);
            }
            catch
            { 
            }
            return maPhieuTra;
        }

        public void LapPhieuTraSach(PhieuTraDTO phieuTraDTO)
        {
            try
            {
                QLTVEntities context = ContextFactory.GetInstance();
                PHIEU_TRA phieuTra = new PHIEU_TRA();                
                phieuTra = CreatePhieuTraEFFromPhieuTraDTO(phieuTraDTO);

                context.AddToPHIEU_TRAList(phieuTra);
                context.SaveChanges();

            }
            catch (System.Exception)
            {

            }
        }

        public DataTable  LayThongTinSachTra(int maDocGia)
        {
            DataTable dbSachTra = new DataTable();
            QLTVEntities context = ContextFactory.GetInstance ();
            int soNgayTraTre;
            int TienPhat;
            dbSachTra.Columns.Add("STT");
            
            try
            {
                var query = from sach in context.SACHList
                            from theLoai in context.THE_LOAIList
                            from phieuMuon in context.PHIEU_MUONList
                            from chiTietPM in context.CT_PHIEU_MUONList
                            from thamSo in context.THAM_SOList
                            where phieuMuon.MaDocGia == maDocGia
                            && sach.TinhTrangSach == 2
                            && chiTietPM.MaPhieuMuon == phieuMuon.MaPhieuMuon
                            && chiTietPM.MaSach == sach.MaSach
                            && sach.MaTheLoai == theLoai.MaTheLoai && thamSo.MaTS == 6
                            select new
                            { 
                                chiTietPM.MaCT_PhieuMuon,
                                sach.MaSach,
                                sach.TenSach,
                                theLoai.TenTheLoai,
                                phieuMuon.NgayMuon,
                                SoNgayMuon = SqlFunctions.DateDiff("Day", phieuMuon.NgayMuon, DateTime.Today)
                            };
                dbSachTra.Columns.Add("MaCT_PhieuMuon");
                dbSachTra.Columns.Add("MaSach");
                dbSachTra.Columns.Add("TenSach");
                dbSachTra.Columns.Add("TenTheLoai");
                dbSachTra.Columns.Add("NgayMuon");
                dbSachTra.Columns.Add("SoNgayMuon");
                dbSachTra.Columns.Add("SoNgayTraTre");
                dbSachTra.Columns.Add("TienPhat");             
                
                
                foreach (var sachMuon in query)
                {
                    DataRow dong = dbSachTra.NewRow();
                    var query1 = (from thamSo in context.THAM_SOList
                                  where thamSo.MaTS == 6
                                  select thamSo).First();
                    if (sachMuon.SoNgayMuon == query1.GiaTri || sachMuon.SoNgayMuon < query1.GiaTri)
                    {
                        soNgayTraTre = 0;
                        TienPhat = 0;
                    }
                    else
                    {
                        soNgayTraTre = Convert.ToInt32(sachMuon.SoNgayMuon - query1.GiaTri);
                        TienPhat = 1000 * soNgayTraTre;
                    }
                    dong["MaCT_PhieuMuon"] = sachMuon.MaCT_PhieuMuon;
                    dong["MaSach"] = sachMuon.MaSach;
                    dong["TenSach"] = sachMuon.TenSach;
                    dong["TenTheLoai"] = sachMuon.TenTheLoai;
                    dong["NgayMuon"] = sachMuon.NgayMuon;
                    dong["SoNgayMuon"] = sachMuon.SoNgayMuon;
                    dong["SoNgayTraTre"] = soNgayTraTre;
                    dong["TienPhat"] = TienPhat;
                    dbSachTra.Rows.Add(dong);                                    
                }                
                DataProvider.ThemSoThuTu(dbSachTra);
                
            }
            catch (System.Exception)
            { 

            }
            return dbSachTra;                         
           
        }

    }
}
